<?php


namespace App\Model\Dao;

use App\Model\Entity\Course;
use Swoft\Bean\Annotation\Mapping\Bean;

/**
 * Class CourseDao
 * @Bean(name="CourseDao")
 * @package App\Model\Dao
 */
class CourseDao
{
    public function add($data){
        //写入数据
        $result = Course::insert($data);
        if($result){
            return true;
        }
        return false;
    }

    /**
     * 实现数据的分页与查询
     * @param $params
     * @return array
     */
    public function getDatas($params)
    {
        $count = Course::count();
        $data = Course::where($params['where'])
            ->forPage(intval($params['page']),intval($params['limit']))->get();
        return ['count'=>$count, 'data'=>$data];
    }

    /**
     * 设置课程的推荐状态
     * @param $data
     * @return bool
     */
    public function setIsShow($data)
    {
        $res = Course::where('id', $data['id'])
            ->update(['isshow'=>$data['isshow']]);
        if ($res){
            return true;
        }
        return false;
    }

    /**
     * 通过ID获取课程信息
     * @param int $id
     * @return Course|bool
     */
    public function getCourseFromId(int $id)
    {
        $course = Course::find($id);
        if($course){
            return $course;
        }
        return false;
    }

    /**
     * 更新入库
     * @param $data
     * @return bool
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     * @throws \Swoft\Db\Exception\DbException
     */
    public function updateData($data){
        //写入数据
        $result = Course::where("id",$data["id"])->update($data["data"]);
        if($result){
            return true;
        }
        return false;
    }

    /**
     * 数据的删除
     * @param $ids
     * @return bool
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     * @throws \Swoft\Db\Exception\DbException
     */
    public function del($ids){
        if(Course::whereIn("id",$ids)->delete()){
            return true;
        }
        return false;
    }
}
